
//存储借阅记录
var borrowRecordsData;
//借书记录查询(两个函数)
function dataBorrowRecord() {
    $("#borrow_record").DataTable({
        destroy:true,
        language: {
            "sProcessing": "处理中...",
            "sLengthMenu": "显示 _MENU_ 项结果",
            "sZeroRecords": "没有匹配结果",
            "sInfo": "显示第 _START_ 至 _END_ 项结果，共 _TOTAL_ 项",
            "sInfoEmpty": "显示第 0 至 0 项结果，共 0 项",
            "sInfoFiltered": "(由 _MAX_ 项结果过滤)",
            "sInfoPostFix": "",
            "sSearch": "搜索:",
            "sUrl": "",
            "sEmptyTable": "表中数据为空",
            "sLoadingRecords": "载入中...",
            "sInfoThousands": ",",
            "oPaginate": {
                "sFirst": "首页",
                "sPrevious": "上页",
                "sNext": "下页",
                "sLast": "末页"
            },
            "oAria": {
                "sSortAscending": ": 以升序排列此列",
                "sSortDescending": ": 以降序排列此列"
            }
        }
    });
}

function borrowRecords() {
    if(borrowRecordsData===undefined){
        $.ajax({
            //几个参数需要注意一下
            type: "GET",//方法类型
            dataType: "json",//预期服务器返回的数据类型'
            headers:{'authorization':header},
            url: "http://120.79.183.142/api/user/borrow/record" ,//url
            success: function (re) {
                if (re["code"] == 200) {

                    var result = re["data"];
                    var num = result.length;
                    for (var index2 = 0; index2 < num; index2++) {
                        var irow = result[index2];
                        console.log(irow);
                        var name = irow["name"];
                        console.log(name);
                        var author = irow["author"];
                        var publisher = irow["publisher"];
                        var created_at = irow["created_at"];
                        var timescale = irow["timescale"];
                        var str = '<tr class="tr_' + index2 + '">' +'<td>' + name + '</td><td>' + author + '</td><td>' + publisher + '</td><td>'
                            + created_at + '</td> <td>' + timescale + '</td></tr> ';
                        $("#borrow_record").append(str);
                    }
                }
                dataBorrowRecord();
            },
            error : function(r) {
                console.log(r);
            }
        });
    }
    else{
        return void(0);
    }


}
//分页显示
function pagination(pno,psize){
    $("#borrowOrReturnRecords").empty();
    var num = borrowRecordsData.length;//表格所有借阅数(所有记录数)
    var irowData = borrowRecordsData;
    var totalPage = 0;//总页数
    var pageSize = psize;//每页显示行数
    //总共分几页
    if(num/pageSize > parseInt(num/pageSize)){
        totalPage=parseInt(num/pageSize)+1;
    }
    else{
        totalPage=parseInt(num/pageSize);
    }
    var currentPage = pno;//当前页数
    var startRow = (currentPage - 1) * pageSize+1;//开始显示的行  31
    var endRow = currentPage * pageSize;//结束显示的行   40
    endRow = (endRow > num)? num : endRow;
    //遍历显示数据实现分页

    for(var i=1;i<(num+1);i++){
        var irow = irowData[i-1];
        if(i>=startRow && i<=endRow){
            var name = irow["name"];
            var state = irow["state"];
            var publisher = irow["publisher"];
            var created_at = irow["created_at"];
            var timescale = irow["timescale"];
            var author = irow["author"];

            var returnButton = "";

            switch (state){
                case 0:
                    state = "在借";
                    break;
                case 1:
                    state = "借出超时";
                    break;
                case 2:
                    state = "超时归还";
                    break;
                case 3:
                    state = "正常归还";
                    break;

            }

            var str = "<div class=\"row\">"
                + "<div class=\"col-xs-3 \">" + name + "</div>"
                + "<div class=\"col-xs-1\">"+"<span>" + author + "</span>" + "</div>"
                + "<div class=\"col-xs-2\">"+publisher + "</div>"
                + "<div class=\"col-xs-2\">"+created_at+"</div>"
                + "<div class=\"col-xs-2\">"+timescale+"</div>"
                + "<div class=\"col-xs-2\">"+state+"</div>"
                + "</div>";

            $("#borrowOrReturnRecords").append(str);
        }
        else {

        }
    }

    var tempStr1 = "共"+num+"条记录 分"+totalPage+"页 当前第"+currentPage+"页";
    var tempStr2 = "";
    if(currentPage>1){
        tempStr2 += "<a href=\"#\" onClick=\"goPage("+(1)+","+psize+paginationTo+")\">首页</a>";
        tempStr2 += "<a href=\"#\" onClick=\"goPage("+(currentPage-1)+","+psize+paginationTo+")\"><上一页</a>"
    }else{
        tempStr2 += "首页 ";
        tempStr2 += "<上一页 ";
    }

    if(currentPage<totalPage){
        tempStr2 += "<a  onClick=\"goPage("+(currentPage+1)+","+psize+paginationTo+")\">下一页></a>";
        tempStr2 += "<a  onClick=\"goPage("+(totalPage)+","+psize+paginationTo+")\">尾页</a>";
    }else{
        tempStr2 += "下一页>";
        tempStr2 += " 尾页";
    }

    document.getElementById("barcon").innerHTML = tempStr1+tempStr2;

}
